#include <iostream>
#include <queue>
#include <cstring>

using namespace std;
typedef pair<int, int> pii;
const int N = 1010;
char a[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m;
int dist[N][N];

void bfs()
{
    queue<pii> q;
    memset(dist, -1, sizeof dist);
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
        {
            if(a[i][j] == '1')
            {
                q.push({i, j});
                dist[i][j] = 0;
            }
        }
    }

    while(q.size())
    {
        pii t = q.front(); q.pop();
        int x = t.first, y = t.second;
        for(int k = 0;k < 4;k ++)
        {
            int a = x + dx[k], b = y + dy[k];
            if(a >= 1 && a <= n && b >= 1 && b <= m && dist[a][b] == -1)
            {
                dist[a][b] = dist[x][y] + 1;
                q.push({a, b});
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            cin >> a[i][j];

    bfs();
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
        {
            cout << dist[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}